package com.service.impl;


import com.dao.CustomerAccountMapper;
import com.dao.CustomerMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.Customer;
import com.model.CustomerAccount;
import com.page.GenericPage;
import com.po.CustomerPO;
import com.po.CustomerQueryPO;
import com.result.Result;
import com.service.CustomerService;
import com.utils.CurrentLoginUserAgent;
import com.vo.CustomerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.testng.collections.Lists;

import java.util.List;

@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {


    @Autowired
    private   CustomerMapper  customerMapper;

    @Autowired
    private CustomerAccountMapper  customerAccountMapper;

    @Override
    public CustomerVO save(CustomerPO customerPO) {
        return null;
    }

    @Override
    public Result<GenericPage<CustomerVO>> queryAll(CustomerQueryPO po) {
        int pageNo = ObjectUtils.isEmpty(po.getPageNo()) ? 1 : po.getPageNo();
        int pageSize =  ObjectUtils.isEmpty(po.getPageSize()) ? 10 : po.getPageSize();
        List<CustomerVO>  voList= Lists.newArrayList();
        //PageHelper.startPage(pageNo, pageSize);
       // Long businessId = CurrentLoginUserAgent.getBusinessId();
        Customer customer = new Customer();
        BeanUtils.copyProperties(po, customer);
        PageInfo<CustomerVO> pageInfo = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(() -> {
            List<Customer> customers = customerMapper.selectAll(customer);
            if(!ObjectUtils.isEmpty(customers)){
                //填充押金
                for (Customer cu: customers) {
                    CustomerVO  vo=new CustomerVO();
                    BeanUtils.copyProperties(cu,vo);
                    CustomerAccount customerAccount = customerAccountMapper.selectByCustomerId(cu.getId());
                    if(!ObjectUtils.isEmpty(customerAccount)){
                        vo.setRecharged(true);
                        vo.setRechargedMoney(customerAccount.getDeposit().doubleValue());
                    }else {
                        vo.setRecharged(false);
                        vo.setRechargedMoney(0.00);
                    }
                    voList.add(vo);
                }
            }
        });

        System.out.println("集合大小是："+voList.size());
        System.out.println(voList);
        System.out.println("分页的总行数是: "+pageInfo.getTotal());
        GenericPage<CustomerVO> genericPage = new GenericPage<>(pageSize, pageInfo.getTotal(), voList);
        return new Result<GenericPage<CustomerVO>>().ok(genericPage);
    }

    @Override
    public Result<Boolean> modifyById(Customer customer) {
        if (ObjectUtils.isEmpty(customer.getId())){
            return new Result<>().error("参数异常");
        }
        int i = customerMapper.updateByPrimaryKeySelective(customer);
        return new Result<>().ok();
    }
}
